package com.hongxin.nw.netty.mq_info;

import com.alibaba.fastjson.JSONObject;
import com.hongxin.nw.netty.model.Transfer;
import com.hongxin.nw.netty.model.analysis.info.Mrena1_17_1;
import com.hongxin.nw.netty.mq_info.domain.CMD110;
import com.hongxin.nw.netty.utils.BinaryTools;
import com.hongxin.nw.netty.utils.JSONUtils;
import com.hongxin.nw.netty.utils.ModifyJsonKeyUtil;
import com.hongxin.nw.netty.values.Catalogue;
import com.hongxin.nw.netty.values.TypeIdentif;

import java.math.BigDecimal;

import static com.hongxin.nw.netty.values.StaticValue.VAL_31;

/**
 * @remark 充电过程信息110
 * @author: GuoXiaolin
 * @date 2019/6/21 19:33
 * @Version 1.0
 * @Descibe
 */
public class ChargeProcessStatus implements IMq {
    @Override
    public JSONObject sendMqInfo(Transfer transfer)  throws Exception{

        //json字段
        String jsonVIN = "VIN";//业务系统需要的字段
        String jsonRemoveVin= "vIN";//业务系统不需要的字段
        String jsonRemoveCurrentSOC= "currentSOC";//业务系统不需要的字段

//        Mrena1_17 dataItem = (Mrena1_17) data.get("data");
        Mrena1_17_1 dataItem = (Mrena1_17_1) transfer.getObj();
        CMD110 cmd110 = new CMD110();
        cmd110.setMachineId(dataItem.getCo());
        cmd110.setCpOrderId(dataItem.getTranNum());
        cmd110.setServiceFee(dataItem.getSzEF());
        cmd110.setParkingFee( dataItem.getZwEF());
        cmd110.setTotalEleFee(dataItem.getZEF());
        cmd110.setTotalFee(dataItem.getChargeFee());
        cmd110.setInterfaceNumber(String.valueOf(Integer.parseInt(dataItem.getChargeIndex())+1));
        cmd110.setChargingTimeRemain(dataItem.getSurplus_time());
        cmd110.setChargingDuration(String.valueOf(Integer.parseInt(dataItem.getChargeTime())*60));
        BigDecimal temp1 = BigDecimal.valueOf(Double.parseDouble(dataItem.getZEndVal()));
        BigDecimal temp2 = BigDecimal.valueOf(Double.parseDouble(dataItem.getZStartVal()));
        cmd110.setChargingElectricQuantity(String.valueOf(temp1.subtract(temp2)));
        cmd110.setStartModel("2".equals(dataItem.getStartModel())? "4" : dataItem.getStartModel());
        cmd110.setUserId(dataItem.getUid());
        if (BinaryTools.isFF(dataItem.getUid(), TypeIdentif.BYTE_USER_ID.getByteLength())){
            cmd110.setUserId("0");
        }

        JSONObject json = (JSONObject) JSONObject.toJSON(cmd110);
        json.put(jsonVIN, dataItem.getCarIdentify());
        json.put(Catalogue.HXCMD,Catalogue.CMD110);
        JSONObject mqJson = ModifyJsonKeyUtil.removeEmptyKey(json);
        mqJson.remove(jsonRemoveVin);
        mqJson.remove(jsonRemoveCurrentSOC);

        return JSONUtils.getRoot(0, VAL_31, mqJson);
    }
}
